package controller;

import com.google.gson.Gson;
import po.*;
import po.Class;
import service.*;
import service.serviceImpl.*;
import util.DBUtils;
import util.DaoRepeat;
import util.ServletRepeat;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "StudentServlet",urlPatterns = "/StudentServlet")
public class StudentServlet  extends BaseServlet {
         private   StudentService service= new StudentServiceImpl();
         private CourseStudentService courseStudentService=new CourseStudentServiceImpl();
         private CourseService courseService=new CourseServiceImpl();
         private ClassService classService=new ClassServiceImpl();
         private GradeService gradeService=new GradeServiceImpl();
         protected void login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
              String username = request.getParameter("username");
             String password = request.getParameter("password");
              Student student = service.queryStudentByNameAndPassword(username, password);
             if(student!=null){
                HttpSession session = request.getSession();
               session.setAttribute("student",student);
                boolean result1=true;
              String json1 = ServletRepeat.getJson(result1);
                 response.getWriter().write(json1);
              }else{
                boolean result2=true;
                 String json2 = ServletRepeat.getJson2(result2);
                 response.getWriter().write(json2);
              }

         }
    protected void queryAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Student student = service.queryStudentByNameAndPassword(username, password);
        Integer classId = student.getClassId();
        Class aClass = classService.queryOne(classId);
        Integer gradeId = aClass.getGid();
        Grade grade = gradeService.queryOne(gradeId);
        Integer collegeId = grade.getCid();
        List<Course> courses = courseService.queryAll(collegeId,gradeId);
        if(courses.size()>0){
            request.setAttribute("courses",courses);
            request.getRequestDispatcher("/queryAllCourse.jsp").forward(request,response);
        }else{
            request.setAttribute("img","暂时无选课信息哟");
            request.getRequestDispatcher("/emptyStudentCourse.jsp").forward(request,response);
        }

    }
    protected void deleteCource(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String i = request.getParameter("cid");
        String s = request.getParameter("sid");
        int cid = Integer.parseInt(i);
        int sid = Integer.parseInt(s);
        CourseStudent courseStudent = courseStudentService.queryOneCourseStudent(cid, sid);
        if(courseStudentService.ifHaveChoose(cid,sid)){
            courseStudentService.delete(courseStudent);
          ServletRepeat.updateNumber(cid);
            boolean result2=true;
            String json2 = ServletRepeat.getJson2(result2);
            response.getWriter().write(json2);
        }else{
            boolean result1=true;
            String json1 = ServletRepeat.getJson(result1);
            response.getWriter().write(json1);
        }


    }
    protected void insertCource(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        CourseStudent courseStudent = new CourseStudent();
        String i = request.getParameter("cid");
        int cid = Integer.parseInt(i);
        String s = request.getParameter("sid");
        int sid = Integer.parseInt(s);
        courseStudent.setCid(cid);
        courseStudent.setSid(sid);
        courseStudentService.insert(courseStudent);
        Integer number = courseStudentService.haveChooseNumber(cid);
        Course course = courseService.queryOne(cid);
        course.setNumber(number);
        courseService.update(course);
        boolean result2=true;
        String json2 = ServletRepeat.getJson2(result2);
        response.getWriter().write(json2);
    }


    protected void ifHaveChoose(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("cid");
        int cid = Integer.parseInt(i);
        String s = request.getParameter("sid");
        int sid = Integer.parseInt(s);
        List<Course> courses = courseService.queryChooseCourse(sid);
        ArrayList<String> times = new ArrayList<>();
        for (Course course : courses) {
            times.add( course.getTIME());
        }
              Course courseTwo= courseService.queryOne(cid);
        String time = courseTwo.getTIME();
        String stringTime= ServletRepeat.coding(time);
        ArrayList<String> objects = new ArrayList<String>();
        for (String timeOne : times) {
            objects.add(ServletRepeat.coding(timeOne));
        }
        int sign=0;
        for (String object : objects) {
            if(String.valueOf(stringTime.charAt(1)).equals(String.valueOf(object.charAt(1)))){
               if(String.valueOf(stringTime.charAt(2)).equals(String.valueOf( object.charAt(2)))||String.valueOf(stringTime.charAt(3)).equals(String.valueOf(object.charAt(3)))){
                   sign=1;
                   break;
               }

            }else{
                sign=2;
                break;
            }
        }
        if(courseService.ifReachBeginTime(cid)){
            if(courseStudentService.ifHaveChoose(cid,sid))
            {
                boolean result1=true;
                String json1 = ServletRepeat.getJson(result1);
                response.getWriter().write(json1);
            }else{
                Course course = courseService.queryOne(cid);
                if(course.getNumber()<course.getTotal()){
                    if(sign!=1){
                    insertCource(request,response);}else{
                        boolean result5=true;
                        String json5 = ServletRepeat.getJson5(result5);
                        response.getWriter().write(json5);
                    }
                }else {
                    boolean result4=true;
                    String json4 = ServletRepeat.getJson4(result4);
                    response.getWriter().write(json4);
                }

            }

        }
      else{
            boolean result3=true;
            String json3 = ServletRepeat.getJson3(result3);
            response.getWriter().write(json3);
        }
    }

    protected void queryMyCourses(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String s = request.getParameter("sid");
        int sid = Integer.parseInt(s);
        List<Course> courses = courseService.queryChooseCourse(sid);
        if(courses.size()>0){
            request.setAttribute("courses",courses);
            request.getRequestDispatcher("/studentCourses.jsp").forward(request,response);
        }else{
            request.setAttribute("img","暂无选课哟");
            request.getRequestDispatcher("/empty.jsp").forward(request,response);
        }


    }

    protected void queryAllStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("classId");
        int classId = Integer.parseInt(id);
        List<Student> students = service.queryAllStudent(classId);
        if(students.size()>0){
            request.setAttribute("classId",classId);
            request.setAttribute("students",students);
            request.getRequestDispatcher("/allStudent.jsp").forward(request,response);
        }else{
            request.setAttribute("classId",classId);
            request.setAttribute("img","暂时无此班级的学生！");
            request.getRequestDispatcher("/emptyStudentList.jsp").forward(request,response);
        }
    }

    protected void addStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("classId");
        int classId = Integer.parseInt(c);
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Student student = new Student();
        student.setUsername(number);
        student.setPassword(password);
        student.setClassId(classId);
        student.setName(name);
        student.setRoleId(1);
        service.addStudent(student);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);

    }

    protected void queryOneStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        Student student = service.queryOneStudent(id);
        request.setAttribute("student",student);
        request.getRequestDispatcher("/updateStudent.jsp").forward(request,response);
    }

    protected void updateStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        String name = request.getParameter("name");
        String c = request.getParameter("classId");
        int classId = Integer.parseInt(c);
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Student student = new Student();
        student.setRoleId(1);
        student.setId(id);
        student.setName(name);
        student.setClassId(classId);
        student.setPassword(password);
        student.setUsername(number);
        service.updateStudent(student);
        boolean result2=true;
        String json2 = ServletRepeat.getJson2(result2);
        response.getWriter().write(json2);
    }
    protected void deleteStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        Student student = new Student();
        student.setId(id);
        Student oneStudent = service.queryOneStudent(id);
        Integer classId = oneStudent.getClassId();
        service.deleteStudent(student);
        List<Student> students = service.queryAllStudent(classId);
        if(students.size()>0){
            request.setAttribute("classId",classId);
            request.setAttribute("students",students);
            request.getRequestDispatcher("/allStudent.jsp").forward(request,response);
        }else{
            request.setAttribute("classId",classId);
            request.setAttribute("img","暂时无此班级的学生！");
            request.getRequestDispatcher("/emptyStudentList.jsp").forward(request,response);
        }

    }

}
